package com.majie.beanDefinition;

import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ChildBeanDefinition;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Objects;

public class BeanDefinitionDemo {

	public static void main(String[] args) {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

		// GenericBeanDefinition可以有父类
		GenericBeanDefinition genericBeanDefinition = new GenericBeanDefinition();
		genericBeanDefinition.setBeanClass(Bean.class);
		genericBeanDefinition.getPropertyValues().add("name", "majie");
		genericBeanDefinition.getPropertyValues().add("age", 18);
		context.registerBeanDefinition("genericBeanDefinition1", genericBeanDefinition);

		GenericBeanDefinition definition2 = new GenericBeanDefinition();
		definition2.setParentName("genericBeanDefinition1");
		// genericBeanDefinition2 的属性继承了 genericBeanDefinition1
		context.registerBeanDefinition("genericBeanDefinition2", definition2);

		// root
		RootBeanDefinition rootBeanDefinition = new RootBeanDefinition();
		rootBeanDefinition.setBeanClass(Bean.class);
		rootBeanDefinition.getPropertyValues().add("name", "xiaoli");
		rootBeanDefinition.getPropertyValues().add("age", 1);
		context.registerBeanDefinition("rootBeanDefinition", rootBeanDefinition);

//		rootBeanDefinition.setParentName("root");   rootBean 不能有parent

		// child
		ChildBeanDefinition childBeanDefinition = new ChildBeanDefinition("rootBeanDefinition");
		context.registerBeanDefinition("childBeanDefinition", childBeanDefinition);

		context.refresh();


		Bean genericBean1 = (Bean) context.getBean("genericBeanDefinition1");
		System.out.println(genericBean1.name);
		System.out.println(genericBean1.age);

		Bean genericBean2 = (Bean) context.getBean("genericBeanDefinition2");
		System.out.println(genericBean2.name);
		System.out.println(genericBean2.age);

		Bean rootBean = (Bean) context.getBean("rootBeanDefinition");
		Bean childBean = (Bean) context.getBean("childBeanDefinition");

		// 返回false 只继承了属性
		System.out.println(genericBean1 == genericBean2);
		System.out.println(rootBean == childBean);


		// BeanDefinitionBuilder  工具
		BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(Bean.class);
		// builder.getBeanDefinition() 其实是一个 GenericBeanDefinition
		context.registerBeanDefinition("bean3", builder.getBeanDefinition());


		// BeanDefinitionHolder 类   ​ 很显然是一个持有者
		// beanDefinition , bean_name, bean_alias(别名的意思就是小名,可以通过小名获取)
		BeanDefinitionHolder holder = new BeanDefinitionHolder(genericBeanDefinition, "bean1", new String[]{"bean2"});
		context.registerBeanDefinition(holder.getBeanName(), holder.getBeanDefinition());
		for (String alias : Objects.requireNonNull(holder.getAliases())) {
			context.registerAlias(holder.getBeanName(), alias);
		}

		// BeanDefinitionParser 类

		// BeanDefinitionReader 类

		// BeanDefinitionRegistry
	}

	static class Bean {
		private String name;

		private int age;

		public String getName( ) {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public int getAge( ) {
			return age;
		}

		public void setAge(int age) {
			this.age = age;
		}
	}
}
